package worker;

import gson.Questions;

import java.util.ArrayList;
import java.util.Random;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.google.gson.Gson;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import data.Data;

public class GetUrl extends Thread {
	DBObject obj;
	public GetUrl(DBObject obj) {

		this.obj = obj;
		this.start();
	}

	public void run(){
		try {
			String charset   = "UTF-8";	
			
			Document doc = null;
			
			ArrayList<String> site = Utils.getStachSite();
			
			String link = "";
			String type = obj.get("type").toString();
			String reglink = obj.get("reglink").toString();
			
			try {
				if(type.equals("html"))
				{
					if(site.contains(Utils.fomatDmain(obj.get("sitemap").toString())))
					{
						ArrayList<String> arr = new ArrayList<String>();
						arr.add("gZl6*KGgFtEiF1MXkz0NuA((");
						arr.add("vxwi93Z)iv36250wzn6aVA((");
						arr.add("zhDn53bzOT9zpIA0vkSdIg((");
						arr.add("hTNoAf7tr6WNrC8jAOlcMQ((");
						
						String str = Utils.GetHtml("https://api.stackexchange.com/2.1/search/advanced?order=desc&answers=1&sort=activity&tagged="+obj.get("tag").toString()+"&site="+Utils.fomatDmain(obj.get("sitemap").toString())+"&filter=!T5TVZso8yZqn*VSo%29T&key="+arr.get(new Random().nextInt(3)), "utf-8");
						
						Gson gson = new Gson();
						
						Questions question = gson.fromJson(str, Questions.class);
						link = question.items.get(0).getLink();
					} else {
						doc = Jsoup.parse(Utils.GetHtml(obj.get("sitemap").toString(), charset));
						
						String[] arrreglink = reglink.split("\\|");
						if(arrreglink.length>=2)
							link = doc.select(arrreglink[0]).get(Integer.parseInt(arrreglink[1])).attr("href");
						else
							link = doc.select(reglink).get(0).attr("href");
					}
					
				}
				else
				{
					try {
						if(site.contains(Utils.fomatDmain(obj.get("sitemap").toString())))
						{
							doc = Jsoup.parse(Utils.GetHtml("http://getstackcontent.appspot.com/stack?link="+obj.get("sitemap").toString(), charset).replaceAll("<link>", "<links>").replaceAll("</link>", "</links>"));
						} else {
							doc = Jsoup.parse(Utils.GetHtml(obj.get("sitemap").toString(), charset).replaceAll("<link>", "<links>").replaceAll("</link>", "</links>"));
						}
						
						link = doc.select("item").get(0).select("links").get(0).text();
					} catch (Exception e) {
						link = doc.select("entry link[rel=alternate]").attr("href");
					}
				}
				
				String base = "";
				try {
					base = doc.select("base").attr("href");
				} catch (Exception e) {
					// TODO: handle exception
				}
				if(base.equals(""))
				{
					link=Utils.fomatLink(link, obj.get("sitemap").toString());
				} else {
					link=Utils.fomatLink(link, base);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			if(!link.equals(""))
			{
				if(obj.get("link")==null || (obj.get("link")!=null && !obj.get("link").toString().equals(link)))
				{
					DBCollection colls = Data.data("savelinks");
					DBObject check = colls.findOne(new BasicDBObject("link",link).append("username", obj.get("username").toString()).append("blogurl", obj.get("blogurl").toString()));
//					System.out.println(check);
					if(check==null)
					{
						obj.put("link", link);
						obj.put("active", "1");
						DBCollection coll = Data.data("urlCraw");
						coll.save(obj);
						
						DBCollection collss = Data.data("logtest");
						BasicDBObject dbObjects = new BasicDBObject();
						dbObjects.put("link", "1");
						collss.insert(dbObjects);
//						System.out.println("1");
						BasicDBObject dbObject = new BasicDBObject();
						dbObject.put("link", link);
						dbObject.put("username", obj.get("username").toString());
						dbObject.put("blogurl", obj.get("blogurl").toString());
						colls.insert(dbObject);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
